﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Proje
{
    class SeatFileConnector
    {
        FileStream file;
        StreamWriter writer;
        StreamReader reader;
        public bool insert(Seat seat,int routeNo) {
            try
            {
                file = new FileStream(FilePath + "\\" + routeNo.ToString(), FileMode.Append);
                writer = new StreamWriter(file);
                string s = createInsertString(seat);
                writer.WriteLine(s);
                writer.Close();
                file.Close();
                return true;
            }
            catch(Exception e) {
                return false;
            }

        }
        public List<Seat> getSeats(int routeNo) {
            try
            {
                List<Seat> liste = new List<Seat>();
                
                file = new FileStream(FilePath + "\\" + routeNo.ToString(), FileMode.OpenOrCreate);
                reader = new StreamReader(file);
                while (!reader.EndOfStream)
                {
                    Seat seat = new Seat();
                    string s = reader.ReadLine();
                    seat.start = s.Split('|')[0];
                    seat.end = s.Split('|')[1];
                    seat.seatNo = Convert.ToInt32(s.Split('|')[2]);
                    seat.gender = s.Split('|')[3];
                    liste.Add(seat);
                }
                return liste;

            }
            catch (Exception e)
            {
                return null;
            }
            finally {
                reader.Close();
                file.Close();
            }
        }


        private string createInsertString(Seat seat)
        {
            return string.Format(
                    "{0}|{1}|{2}|{3}",
                    seat.start,
                    seat.end,
                    seat.seatNo,
                    seat.gender
                );
        }

        private static string SettingsFolder
        {
            get
            {
                string folder = @"C:\Users\Serhat\Desktop\Proje\Koltuk";

                if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
                return folder;
            }

        }


        string FilePath
        {
            get
            {
                return Path.Combine(SettingsFolder, "");

            }
        }
    }
}
